﻿namespace TPXSDK.NET.UAV;

/// <summary>
/// 动态属性类，支持变更通知，动态注册
/// </summary>
public class Property<T>
{
    public Property(string? name)
    {
        Name = string.IsNullOrEmpty(name) ? typeof(T).Name : name;
    }

    public string Name { get; }

    public override string ToString()
    {
        return _value != null ? $"{Name}:{_value}:{_value.GetType()}" : $"{Name}:?:?";
    }

    public T? Value
    {
        set
        {
            lock (this)
            {
                if (_value != null && _value.Equals(value))
                {
                    return;
                }
                _value = value;
            }
            NotifyChanged();
        }
        get => _value;
    }

    /// <summary>
    /// 用于通知属性更改
    /// </summary>
    public void NotifyChanged()
    {
        Changed?.Invoke(Name);
    }

    #region 成员变量
    public delegate void PropertyChangedHandler(string name);
    public event PropertyChangedHandler? Changed;
    private T? _value;
    #endregion
}
